package com.spring.sunflower.userinfo;

import com.blankj.utilcode.util.LogUtils;
import com.spring.sunflower.base.BaseObserver;
import com.spring.sunflower.base.BasePresenter;
import com.spring.sunflower.bean.AnchorDetailBean;
import com.spring.sunflower.bean.LabelBean;
import com.spring.sunflower.bean.STSCertBean;
import com.spring.sunflower.bean.UserAuthBean;
import com.spring.sunflower.bean.UserInfoUpdateBean;
import com.spring.sunflower.common.CommonContract;

import java.util.Map;

import io.reactivex.rxjava3.core.Observable;

/**
 * Created by Kevin on 2021/6/28<br/>
 * <p>
 * Describe:<br/>
 */
public class UserInfoEditPresenter extends BasePresenter<CommonContract.UserInfoEditView> {
    public UserInfoEditPresenter(CommonContract.UserInfoEditView baseView) {
        super(baseView);
    }

    public void getDetail(String targetId) {
        LogUtils.wTag("BasePresenter", "targetId=" + targetId);
        Observable<AnchorDetailBean> observable = requestDataSource.requestAnchorDetail(targetId);
        addDisposable(observable, new BaseObserver<AnchorDetailBean>(baseView) {

            @Override
            public void onSuccess(AnchorDetailBean bean) {
                AnchorDetailBean.DatasBean datas = bean.getDatas();
                baseView.hideLoading();
                baseView.showData(datas);
            }

            @Override
            public void onError(int status, String msg) {

            }
        });
    }

    public void checkAuth() {
        Observable<UserAuthBean> observable = requestDataSource.checkApplyAuth();
        addDisposable(observable, new BaseObserver<UserAuthBean>(baseView) {

            @Override
            public void onSuccess(UserAuthBean bean) {
                baseView.hideLoading();
                UserAuthBean.DatasBean datas = bean.getDatas();
                Boolean authenticate = datas.getAuthenticate();
                Boolean personalInfo = datas.getPersonalInfo();
                baseView.personInfoFinish(personalInfo);
                baseView.realAuth(authenticate);

            }

            @Override
            public void onError(int status, String msg) {
            }
        });
    }

    public void getLabel(String type) {
        Observable<LabelBean> observable = requestDataSource.requestLabel(type);
        addDisposable(observable, new BaseObserver<LabelBean>(baseView) {

            @Override
            public void onSuccess(LabelBean bean) {
//                LabelBean recommendBean = handleJson(o, LabelBean.class);
                baseView.hideLoading();
                baseView.showLabelData(bean.getDatas(), type);
            }

            @Override
            public void onError(int status, String msg) {

            }
        });
    }

    public void updateUserInfo(Map<String, String> map) {
        Observable<UserInfoUpdateBean> observable = requestDataSource.userInfoUpdate(map);
        addDisposable(observable, new BaseObserver<UserInfoUpdateBean>(baseView) {

            @Override
            public void onSuccess(UserInfoUpdateBean bean) {
                baseView.updateSuccess(bean.getDatas());
            }

            @Override
            public void onError(int status, String msg) {
                baseView.updateFailed();
            }
        });
    }

    public void updateUserInfo(Map<String, String> map, boolean isUpdateAvatar) {
        Observable<UserInfoUpdateBean> observable = requestDataSource.userInfoUpdate(map);
        addDisposable(observable, new BaseObserver<UserInfoUpdateBean>(baseView) {

            @Override
            public void onSuccess(UserInfoUpdateBean bean) {
                baseView.updateSuccess(bean.getDatas(),isUpdateAvatar);
            }

            @Override
            public void onError(int status, String msg) {
                baseView.updateFailed();
            }
        });
    }

    public void getSTSCert() {
        Observable<STSCertBean> observable = requestDataSource.getSTSCert();
        addDisposable(observable, new BaseObserver<STSCertBean>(baseView) {

            @Override
            public void onSuccess(STSCertBean bean) {
                STSCertBean.DatasBean datas = bean.getDatas();
                baseView.loadSTSCert(datas);
            }

            @Override
            public void onError(int status, String msg) {

            }
        });
    }
}
